﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using SZHL.UI.AppCode;
using FastReflectionLib;
using SZHL.Data;
using Newtonsoft.Json;
using System.Data;

namespace SZHL.UI.API
{
    public class LSGLManage : IWsService
    {
        public void ProcessRequest(HttpContext context, ref Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            MethodInfo methodInfo = typeof(LSGLManage).GetMethod(msg.Action.ToUpper());
            LSGLManage model = new LSGLManage();
            methodInfo.FastInvoke(model, new object[] { context, msg, P1, P2, UserInfo });
        }
        /// <summary>
        /// 添加老师
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="UserInfo"></param>
        public void ADDLSGL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            KZ_LSGL lsgl = JsonConvert.DeserializeObject<KZ_LSGL>(P1);
            if (lsgl.LS_LSXM.Trim() == "")
            {
                msg.ErrorMsg = "老师姓名不能为空";
                return;
            }
            if (lsgl.ID == 0)
            {
                lsgl.CRDate = DateTime.Now;
                lsgl.CRUser = UserInfo.User.UserName;
                new KZ_LSGLB().Insert(lsgl);
            }
            else
            {
                new KZ_LSGLB().Update(lsgl);
            }
            msg.Result = lsgl;
        }
        /// <summary>
        /// 获取老师列表
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETLSGLLIST(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            int page = int.Parse(context.Request["p"]);
            string branchCode = context.Request.QueryString["b"] ?? "";
            string strWhere = "";
            if (branchCode != "")
            {
                string strQueryUser = context.Request["QUSER"] ?? "";
                if (strQueryUser == "")
                {
                    strQueryUser = new JH_Auth_BranchB().GetAllUserByBranch(branchCode);
                }
                strWhere += string.Format("  CRUser in ('{0}')", strQueryUser.ToFormatLike());
            }
            else
            {
                strWhere += string.Format("  CRUser in ('{0}')",UserInfo.User.UserName);
            }
            string content = context.Request["Content"];
            if (content != "")
            {
                strWhere += string.Format(" And  LS_LSXM like '%{0}%'", content);
            }
            int recordCount = 0;
            DataTable dt = new ZH_GZRZB().GetDataPager("KZ_LSGL", "[ID],[LS_LSXM],[LS_LSTP],[LS_LSJS],[LS_LSLXFS],[LS_LSXC],[LS_BeiZhu],[LS_Type],[FileS],[CRUser],[CRDate],[LS_Address],[LS_ZYKC] ", 8, page, "CRDate desc", strWhere, ref recordCount);

            msg.Result = dt;
            msg.Result1 = Math.Ceiling(recordCount * 1.0 / 8);

        }
        /// <summary>
        /// 获取老师信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void GETLSGLMODEL(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            msg.Result = new KZ_LSGLB().GetEntity(d => d.ID.ToString() == P1); 
        }
            /// <summary>
        /// 删除老师
        /// </summary>
        /// <param name="context"></param>
        /// <param name="msg">公告类型</param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="strUserName"></param>
        public void DELLSGLBYID(HttpContext context, Msg_Result msg, string P1, string P2, JH_Auth_UserB.UserInfo UserInfo)
        {
            try
            {
                if (!new KZ_LSGLB().Delete(d => d.ID.ToString() == P1)) {

                    msg.ErrorMsg = "删除失败";
                }
            }
            catch (Exception ex)
            {
                msg.ErrorMsg = ex.Message;
            }
        }
        

    }
}